using SS;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SM.core
{
	internal class Group
	{
		private Func objFunc = new Func();
		public Group()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public int action(int GID, string GName, string GPer, int GStatus)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabGroup", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("GID", SqlDbType.Int);
				sqlCommand.Parameters.Add("GName", SqlDbType.NVarChar, 300);
				sqlCommand.Parameters.Add("GPer", SqlDbType.VarChar, 500);
				sqlCommand.Parameters.Add("GStatus", SqlDbType.SmallInt);
				sqlCommand.Parameters["GID"].Value = GID;
				sqlCommand.Parameters["GName"].Value = GName;
				sqlCommand.Parameters["GPer"].Value = GPer;
				sqlCommand.Parameters["GStatus"].Value = GStatus;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public void loadToListView(ListView lv)
		{
			lv.Items.Clear();
			int num = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT GID,GName,GStatus FROM [TabGroup] ORDER BY [GID]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				lv.Items.Add(sqlDataReader["GID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["GName"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["GStatus"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void loadPerToListView(ListView lv)
		{
			lv.Items.Clear();
			int num = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT PerID,PerName FROM [TabPer] WHERE [PStatus] = 1 ORDER BY [POrder]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				lv.Items.Add(sqlDataReader["PerID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["PerName"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void checkPer(string gid, ListView lv)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT * FROM [TabGroup] WHERE [GID] = " + gid;
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				string text = sqlDataReader["GPer"].ToString();
				for (int i = 0; i < lv.Items.Count; i++)
				{
					lv.Items[i].Checked = false;
				}
				for (int i = 0; i < lv.Items.Count; i++)
				{
					if (text.Contains("\"" + lv.Items[i].SubItems[0].Text + "\""))
					{
						lv.Items[i].Checked = true;
					}
				}
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int delItem(int gid)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string cmdText = "DELETE [TabGroup] WHERE [Gid] = " + gid;
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public bool checkExist(string ID)
		{
			bool result = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT UserName FROM [TabUser] WHERE [Group]='" + ID + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				result = true;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
	}
}
